#include "chatbubbleandhead.h"

ChatBubbleAndHead::ChatBubbleAndHead(bool isSender,QPixmap senderhead, QPixmap receiverhead,ChatWidget* prt, QWidget *parent)
    : QWidget(parent)
{
    qDebug() << "new chatBandH";
    content = new ChatBubble(isSender, prt);
    QLabel* head = new QLabel(this);
    head->setFixedSize(50, 50);

    // 区分发送方和接收方;
    if(isSender){
        // 圆形头像显示
//        head->setPixmap(senderhead);
        senderhead = senderhead.scaled(head->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

        int width = head->size().width();
        int height = head->size().height();

        QPixmap image(width, height);
        image.fill(Qt::transparent);

        QPainterPath painterPath;
        painterPath.addEllipse(0, 0, width, height);

        QPainter painter(&image);
        painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
        painter.setClipPath(painterPath);
        painter.drawPixmap(0, 0, width, height, senderhead);
        //绘制到label
        head->setPixmap(image);
    }
    else{
//        head->setPixmap(receiverhead);
        // 圆形头像显示
        receiverhead = receiverhead.scaled(head->size(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation);

        int width = head->size().width();
        int height = head->size().height();

        QPixmap image(width, height);
        image.fill(Qt::transparent);

        QPainterPath painterPath;
        painterPath.addEllipse(0, 0, width, height);

        QPainter painter(&image);
        painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
        painter.setClipPath(painterPath);
        painter.drawPixmap(0, 0, width, height, receiverhead);
        //绘制到label
        head->setPixmap(image);
    }
    QHBoxLayout* headAndMessage = new QHBoxLayout;
    if(!isSender){//非发送者，头像在左边（相对下面的content)
        headAndMessage->addWidget(head);
     qDebug() << "sender!";
    }
//    headAndMessage->addLayout(layout);
    headAndMessage->addWidget(content);
    if(isSender){//发送者，头像在右边
        headAndMessage->addWidget(head);
    }
    this->setLayout(headAndMessage);
}

void ChatBubbleAndHead::set_txt(QString show_message){
    content->set_txt(show_message);
}
void ChatBubbleAndHead::set_file(const QString& fileName,const QByteArray& info,const int& sz){
    content->set_file(fileName,info,sz);
}
void ChatBubbleAndHead::set_picture(QPixmap picture){
    content->set_picture(picture);
}
